package cn.com.surker.sale.service.impl;

import cn.com.surker.common.Code;
import cn.com.surker.sale.dao.SaleStrengthSpecialPriceDao;
import cn.com.surker.sale.entity.SaleStrengthSpecialPrice;
import cn.com.surker.sale.service.SaleStrengthSpecialPriceBaseService;
import cn.com.surker.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author lucky
 * @create 2024年07月24 10:29
 * @desc
 */
public abstract class SaleStrengthSpecialPriceBaseServiceImpl extends ServiceImpl<SaleStrengthSpecialPriceDao, SaleStrengthSpecialPrice> implements SaleStrengthSpecialPriceBaseService {

    @Resource
    protected SaleStrengthSpecialPriceDao saleStrengthSpecialPriceDao;

    /**
     * 新增砼特性单价信息
     * @return
     */
    public Integer saveSpecialPrice(SaleStrengthSpecialPrice specialPrice) {
        if (specialPrice == null)
            return Code.PARAM_EMPTY.getCode();
        if (!StringUtils.isNull(specialPrice.getEquirement()))
            specialPrice.setEquirement(specialPrice.getEquirement().toUpperCase());
        SaleStrengthSpecialPrice ss = saleStrengthSpecialPriceDao.findTakeTime(specialPrice.getOrgId(), specialPrice.getContractId(), specialPrice.getEquirement());
        if (ss != null)
            return Code.EXE_CONTRACT_SUBSIDY.getCode();
        Integer code = saleStrengthSpecialPriceDao.insert(specialPrice);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 修改砼特性单价信息
     * @return
     */
    public Integer updateSpecialPrice(SaleStrengthSpecialPrice specialPrice) {
        if (specialPrice == null || specialPrice.getId() == null)
            return Code.PARAM_EMPTY.getCode();
        Integer code = saleStrengthSpecialPriceDao.updateById(specialPrice);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 禁用
     * @param id
     * @param status
     * @return
     */
    public Integer updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isAllParamNull(id, status))
            return Code.PARAM_EMPTY.getCode();
        SaleStrengthSpecialPrice specialPrice = saleStrengthSpecialPriceDao.selectById(id);
        if (specialPrice == null)
            return Code.ERROR_DATA.getCode();
        specialPrice.setStatus(status);
        specialPrice.setUpdator(creator);
        specialPrice.setUpdateTime(new Date());
        Integer code = saleStrengthSpecialPriceDao.updateById(specialPrice);
        if (code > 0)
            return 0;
        return -1;
    }

    /**
     * 查询合同特性价格
     * @param contractId
     * @return
     */
    public List<SaleStrengthSpecialPrice> findByContract(Long contractId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<SaleStrengthSpecialPrice> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        List<SaleStrengthSpecialPrice> list = saleStrengthSpecialPriceDao.selectList(query);
        if (list == null || list.size() == 0)
            return null;
        return list;
    }
}
